﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security;

namespace WindowsLiveID
{
    public partial class Logon : Form
    {
        public Logon()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Allows you to set the illustrative image on the logon dialog.
        /// </summary>
        public Image Illustration
        {
            get { return IllustrationBox.Image; }
            set { IllustrationBox.Image = value; }
        }

        private void DisplayError(string text)
        {
            MessageBox.Show(text, "Logon Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void SignIn_Click(object sender, EventArgs e)
        {
            try
            {
                WindowsLive.Instance.Authenticate(Address.Text, Password.Text, RememberMe.Checked, RememberPassword.Checked);
            }
            catch (Exception ex)
            {
                DisplayError(ex.Message);
                return;
            }
            
            WindowsLive.Instance.SignInAutomatically = SignInAutomatically.Checked;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void SignUp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("https://signup.live.com/signup.aspx");
        }

        private void Logon_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(WindowsLive.Instance.PersistedAddress))
            {
                RememberMe.Checked = true;
                RememberPassword.Enabled = true;
                SignInAutomatically.Enabled = true;

                Address.Text = WindowsLive.Instance.PersistedAddress;

                if (!string.IsNullOrEmpty(WindowsLive.Instance.PersistedPassword))
                {
                    RememberPassword.Checked = true;
                    Password.Text = Bitter.Protect.UnprotectText(WindowsLive.Instance.PersistedPassword, WindowsLive.Instance.PersistedAddress);
                }
            }

            SignInAutomatically.Checked = WindowsLive.Instance.SignInAutomatically;
        }

        private void RememberMe_CheckedChanged(object sender, EventArgs e)
        {
            RememberPassword.Enabled = RememberMe.Checked;
            SignInAutomatically.Enabled = RememberMe.Checked;
        }
    }
}
